﻿/// <reference path="../../scripts/jquery-1.11.2.min.js" />
/// <reference path="../../scripts/bootstrap.min.js" />

if (typeof ($) != "undefined" && $) {
    //验证用户
    if ($.cookie('openid') == null || $.cookie('openid') == '') {
        window.location.href = 'empvalidate.html';
    }
    else {
        $.ajax({
            type: "get",
            url: "api/login.ashx?ac=isactive&k=" + Math.random(),
            error: function () {
                window.location.href = 'empactive.html';
            },
            success: function (data) {
                var obj = eval("(" + data + ")").result;
                if (obj.r == "suc") {
                    $(document).ready(onReady);
                }
                else {
                    window.location.href = 'empactive.html';
                }
            }
        });
    }
}

function onReady() {
    //滑动
    var tabsSwiper = new Swiper('.swiper-container', {
        speed: 500,
        onSlideChangeStart: function () {
            $(".tabs .active").removeClass('active');
            $(".tabs a").eq(tabsSwiper.activeIndex).addClass('active');
        }
    });
    $(".tabs a").on('touchstart mousedown', function (e) {
        e.preventDefault()
        $(".tabs .active").removeClass('active');
        $(this).addClass('active');
        tabsSwiper.swipeTo($(this).index());
    });
    $(".tabs a").click(function (e) {
        e.preventDefault();
    });

    $.ajax({
        type: "get",
        url: "api/order.ashx?ac=empinfo&k=" + Math.random(),
        beforeSend: function () {
            $('#loadingToast').show();
        },
        error: function () {
            showToast("员工信息加载失败");
        },
        success: function (data) {
            var obj = eval("(" + data + ")");
            $("#userID").html(obj.userid);
            $("#userName").html(obj.name);

            loadData(4, 'waitList');
            loadData(2, 'workList');
            loadData(3, 'comList');
        },
        complete: function () {
            $('#loadingToast').hide();
        }
    });
}

function loadData(state, liname) {
    var data = { state: state };
    $.ajax({
        type: "get",
        data: data,
        url: "api/order.ashx?ac=emplist&k=" + Math.random(),
        success: function (data) {
            var list = eval("(" + data + ")");
            $("#" + liname + "").children().remove();
            for (var i = 0; i < list.length; i++) {
                var o = list[i];
                var htm = '<div class="weui_cells" style="margin-top:0;margin-bottom:10px" id="div' + o.OrderID + '">';
                htm += '<div class="weui_cell">';
                htm += '<div class="weui_cell_bd weui_cell_primary">订单号：' + o.OrderNo + '</div>';
                htm += '<div class="weui_cell_no" lang="state">' + o.StateName + '</div>';
                htm += '</div>';
                htm += '<div class="weui_cell">';
                htm += '<div class="weui_cell_bd weui_cell_primary fz13 fc6">';
                htm += o.TrueName + ' <a href="tel:' + o.Mobile + '" class="fz15">' + o.Mobile + '</a><br />';
                htm += o.CarNumber + ' [' + o.CarType + ' - ' + o.CarColor + ']<br />';
                if (o.Address.length > 12) {
                    htm += o.Address.substring(0, 12) + '<br />';
                    htm += o.Address.substring(12) + '<br />';
                }
                else {
                    htm += o.Address + '<br />';
                }
                htm += o.Interior == 1 ? ' 外观+内饰简洗' : ' 外观';
                htm += '<br />';
                htm += '付款时间：<br />' + o.PayDate;
                htm += '</div>';
                if (o.State == '4') {
                    htm += '<div class="weui_cell_ft fz13 fc6">' + o.UserName + '(工号：' + padLeft(o.UserID, 4) + ')<br />';
                }
                else if (o.State == '2') {
                    htm += '<div class="weui_cell_ft fz13 fc6">' + o.UserName + '(工号：' + padLeft(o.UserID, 4) + ')<br />开始时间：<br />' + o.StartTime + '</div>';
                }
                else if (o.State == '3') {
                    htm += '<div class="weui_cell_ft fz13 fc6">' + o.UserName + '(工号：' + padLeft(o.UserID, 4) + ')<br />开始时间：<br />' + o.StartTime + '<br />完成时间：<br />' + o.EndTime + '</div>';
                }
                htm += '</div>';

                if (o.State == '4') {
                    htm += '<div class="weui_cell">';
                    htm += '<div class="weui_cell_bd weui_cell_primary fz13 fc6">&nbsp;</div>';
                    htm += '<div class="weui_cell_no">';
                    htm += '<button type="button" class="btn btn-success btn-sm" onclick="Start(' + o.OrderID + ')">开始洗车</button> ';
                    htm += '<button type="button" class="btn btn-warning btn-sm" onclick="Cancel(' + o.OrderID + ',' + o.CouponsID + ')">取消</button> ';
                }
                else if (o.State == '2') {
                    htm += '<div class="weui_cell">';
                    htm += '<div class="weui_cell_bd weui_cell_primary fz13 fc6" style="height:30px">&nbsp;</div>';
                    htm += '<div class="weui_cell_no">';
                    htm += '<button type="button" class="btn btn-info btn-sm" onclick="Complete(' + o.OrderID + ')">完成洗车</button> ';
                }
                htm += '</div>';
                htm += '</div>';
                htm += '</div>';

                $("#" + liname + "").append(htm);
            }
            if (list.length == 0) {
                $("#" + liname + "").append('<div class="width100 center"><img src="images/none.png" /></div>');
            }
            var oldheight = $("#list").height();
            var newheight = list.length * 220;
            $("#list").height(oldheight < newheight ? newheight : oldheight);
        }
    });
}
//刷新
function reload() {
    loadData(4, 'waitList');
    loadData(2, 'workList');
    loadData(3, 'comList');
}
//开始洗车
function Start(id) {
    var data = { id: id };
    $.ajax({
        type: "post",
        data: data,
        url: "api/order.ashx?ac=start&k=" + Math.random(),
        beforeSend: function () {
            $('#loadingToast').show();
        },
        error: function () {
            showToast("操作失败");
        },
        success: function (data) {
            var obj = eval("(" + data + ")").result;
            if (obj.r == "suc") {
                loadData(4, 'waitList');
                loadData(2, 'workList');
                showToast(obj.val);
            }
            else {
                loadData(4, 'waitList');
                showToast(obj.val);
            }
        },
        complete: function () {
            $('#loadingToast').hide();
        }
    });
}
//取消
function Cancel(id, couponsid) {
    if (confirm("您确定要取消订单吗？")) {
        var data = { id: id, couponsid: couponsid };
        $.ajax({
            type: "post",
            data: data,
            url: "api/order.ashx?ac=empcancel&k=" + Math.random(),
            beforeSend: function () {
                $('#loadingToast').show();
            },
            error: function () {
                showToast("操作失败");
            },
            success: function (data) {
                var obj = eval("(" + data + ")").result;
                if (obj.r == "suc") {
                    loadData(4, 'waitList');
                    showToast(obj.val);
                }
                else {
                    showToast(obj.val);
                }
            },
            complete: function () {
                $('#loadingToast').hide();
            }
        });
    }
}
//完成
function Complete(id) {
    var data = { id: id };
    $.ajax({
        type: "post",
        data: data,
        url: "api/order.ashx?ac=cmplete&k=" + Math.random(),
        beforeSend: function () {
            $('#loadingToast').show();
        },
        error: function () {
            showToast("操作失败");
        },
        success: function (data) {
            var obj = eval("(" + data + ")").result;
            if (obj.r == "suc") {
                loadData(2, 'workList');
                loadData(3, 'comList');
                showToast(obj.val);
            }
            else {
                showToast(obj.val);
            }
        },
        complete: function () {
            $('#loadingToast').hide();
        }
    });
}